home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / 0748.ZIP / ABBREV < prev    next >
Text File  |  1987-07-04  |  7KB  |  210 lines

  1. /*-----------------------------------------------------------------
  2. |                           ABBREV                                |
  3. | This macro sorta simulates the abbrev facility that was made    |
  4. | popular in EMACS, and was later commercialized even further in  |
  5. | packages like Productivity Plus (TM). It also can be considered |
  6. | to be a way to further enhance the editor's keyboard macro      |
  7. | facility.                                                       |
  8. |                                                                 |
  9. | The main idea is that we have a buffer where each line contains |
  10. | an abbreviation, a space, and the abbrev's expansion. When we   |
  11. | are editing text, we can type an abbrev, hit a function key, and|
  12. | have the abbrev expanded automatically. (Note - instead of a    |
  13. | function key, you might optionally program this macro to be     |
  14. | invoked when you hit the space bar.)                            |
  15. |                                                                 |
  16. | For instance, if you wanted the word "ii" to be an abbrev for   |
  17. | the phrase "Institute of Intermediate Ignoramuses", you could   |
  18. | enter this abbrev and expansion in the abbrev buffer. When      |
  19. | you typed "ii" and then the EXPAND ABBREV function key, "ii"    |
  20. | would be replaced by "Institute of ...".                        |
  21. |                                                                 |
  22. | This package allows you to create/load and write an abbrev      |
  23. | buffer. You may add new abbrevs and delete existing abbrevs.    |
  24. | Of course, you may want to add other functions which can operate|
  25. | of abbrevs. For this, you need the macro compiler.              |
  26. | The first extension which comes to mind is support for multiple |
  27. | abbrev buffers.                                                 |
  28. |                                                                 |
  29. | ENJOY!!!!                                                       |
  30. |                                                                 |
  31. | <<<< Marc Adler  7/87 >>>>                                      |
  32. -----------------------------------------------------------------*/
  33.  
  34. #define ALT_A  158
  35. #define ALT_E  146
  36. #define ALT_I  151
  37.  
  38. int  CurrAbbrevBuf;
  39. int  CurrAbbrevLine;
  40.  
  41. init()
  42. {
  43.   assign_key("load_abbrev",   ALT_A);   /* ALT A creates/loads an abbrev */
  44.   assign_key("expand_abbrev", ALT_E);   /* ALT E expands an abbrev    */
  45.   assign_key("insert_abbrev", ALT_I);   /* ALT I inserts a new abbrev */
  46. }
  47.  
  48.  
  49. /* expand_abbrev - expands an abbrev when you press ALT E. We assume that */
  50. /*                 the cursor is just to the right of the abbrev to expand*/
  51. expand_abbrev()
  52. {
  53.   string abbrev,
  54.          defn;
  55.  
  56.   /* back up and extract the abbrev from the text */
  57.   origcol = currcol();
  58.   prevword();
  59.   startcol = currcol();
  60.   
  61.   abbrev = substr(currline(), startcol, origcol - startcol);
  62.   defn   = search_abbrev(abbrev);
  63.   
  64.   if (defn != "")       /* it was found */
  65.   {
  66.     delword();          /* get rid of the abbrev */
  67.     insert(defn);       /* and replace it with the expansion */
  68.   }
  69.   else                  /* abbrev not found */
  70.   {
  71.     setcol(origcol);    /* restore the cursor */
  72.     bell();
  73.   }
  74. }
  75.  
  76. /* search_abbrev - searches the current abbrev buffer for the requested */
  77. /*                 abbrev. Return the NULL string if not found.         */
  78. search_abbrev(candidate)
  79.   string candidate;
  80. {
  81.   /* Go to the start of the abbrev buffer */
  82.   oldbuf = currbuf();
  83.   setcurrbuf(CurrAbbrevBuf);
  84.   gobof();
  85.  
  86.   if (fsearch(pattern = sprintf("^%s ", candidate)) > 0)    /* found */
  87.   {
  88.     CurrAbbrevLine = currlinenum();
  89.     defn = substr(currline(), strlen(pattern), 128);
  90.     setcurrbuf(oldbuf);
  91.     return defn;
  92.   }
  93.   else          /* not found */
  94.   {
  95.     CurrAbbrevLine = 0;
  96.     setcurrbuf(oldbuf);
  97.     return "";
  98.   }
  99. }
  100.  
  101.  
  102. /* insert_abbrev - inserts an abbrev and the expansion in the current */
  103. /*                 abbrev buffer.                                     */
  104. insert_abbrev()
  105. {
  106.   string abbrev, defn;
  107.   
  108.   if ((abbrev = get_tty_str("Abbrev to insert : ")) == "")
  109.     return;
  110.  
  111.   /* Search the abbrev buffer for a duplicate entry */
  112.   search_abbrev(abbrev);
  113.   if (CurrAbbrevLine != 0)
  114.   {
  115.      c = get_tty_str("Abbrev already exists - replace it? (Y/N)");
  116.      if (c != "y" && c != "Y")
  117.        return;
  118.   }
  119.  
  120.   /* Prompt the user for the expansion */
  121.   if ((defn = get_tty_str("Expansion : ")) == "")
  122.     return;
  123.   
  124.   oldbuf = currbuf();
  125.   setcurrbuf(CurrAbbrevBuf);
  126.  
  127.   if (CurrAbbrevLine == 0)      /* inserting a new abbrev at the eof */
  128.   {
  129.     goeof();
  130.     if (currline() != "")
  131.       insert("\n");
  132.     insert(sprintf("%s %s", abbrev, defn));
  133.   }
  134.   else                          /* replacing an existing entry */
  135.   {
  136.     goline(CurrAbbrevLine);
  137.     deleol();
  138.     insert(sprintf("%s %s", abbrev, defn));
  139.   }
  140.  
  141.   setcurrbuf(oldbuf);
  142. }
  143.  
  144. /* delete_abbrev - deletes an abbrev from the abbrev buffer */
  145. delete_abbrev()
  146. {
  147.   string abbrev;
  148.   
  149.   if ((abbrev = get_tty_str("Abbrev to delete : ")) == "")
  150.     return;
  151.   search_abbrev(abbrev);
  152.   if (CurrAbbrevLine == 0)      /* Abbrev not found */
  153.   {
  154.     bell();
  155.     get_tty_str("The abbrev wasn't found");
  156.   }
  157.   else                          /* The abbrev was found */
  158.   {
  159.     oldbuf = currbuf();         /* save the curr buffer id */
  160.     setcurrbuf(CurrAbbrevBuf);  /* go to the abbrev buffer */
  161.     goline(CurrAbbrevLine);     /* and the delete the line */
  162.     delline();
  163.     setcurrbuf(oldbuf);
  164.   }
  165. }
  166.  
  167. /************************* I/O OPERATIONS ***********************/
  168.  
  169. /* load_abbrev - creates a new abbrev buffer, or loads in an existing one.*/
  170. /* Abbrev files will have the default extension of ABV. */
  171. load_abbrev()
  172. {
  173.   string fname;
  174.   
  175.   if ((fname = get_tty_str("Abbrev file : ")) == "")    /* prompt the user */
  176.     return;
  177.   if (!index(fname, "."))            /* User didn't supply an extension */
  178.     fname = strcat(fname, ".ABV");   /* Add the ABV extension           */
  179.  
  180.   oldbuf = currbuf();                /* save the current buffer id      */
  181.  
  182.   setcurrbuf(abuf = create_buffer(fname));      /* create/load the buffer */
  183.   if (currline() == "")                         /* a new buffer? */
  184.     get_tty_str("New abbrev buffer");
  185.   else
  186.     get_tty_str("Abbrev file loaded");
  187.   CurrAbbrevBuf = abuf;
  188.  
  189.   setcurrbuf(oldbuf);                 /* Restore the original editing buffer */
  190. }
  191.  
  192.  
  193. /* write_abbrev - writes the current abbrev buffer to a file */
  194. write_abbrev()
  195. {
  196.   string fname;
  197.  
  198.   oldbuf = currbuf();
  199.  
  200.   /* Get the current abbrev buffer's name, maybe concat */
  201.   /* the ABV extension and dump it to a file.           */
  202.   setcurrbuf(CurrAbbrevBuf);
  203.   fname = filename();
  204.   if (!index(fname, "."))
  205.     fname = strcat(fname, ".ABV");
  206.   writefile(fname);
  207.  
  208.   setcurrbuf(oldbuf);
  209. }
  210.